﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Customer_Profile.SuperUpdate.DBDao;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.SuperUpdate
{
    public partial class DealsInfor : System.Web.UI.Page
    {
        string deal_no = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["deal_no"] != null)
            {
                deal_no = Request["deal_no"].ToString();
            }
            else
            {
                lblWarning.Text = "交易号不能为空";
                return;
            }
            if (!Page.IsPostBack)
            {
                DataSet dsProd = ProductDao.getAllProduct("ALL");
                dropProd.DataSource = dsProd;
                dropProd.DataTextField = "procduct_name";
                dropProd.DataValueField = "procduct_code";
                dropProd.DataBind();
                //dropDueProd
                dropDueProd.DataSource = dsProd;
                dropDueProd.DataTextField = "procduct_name";
                dropDueProd.DataValueField = "procduct_code";
                dropDueProd.DataBind();
                ListItem liAll = new ListItem();
                liAll.Text = "取款";
                liAll.Value = "";
                dropDueProd.Items.Insert(0, liAll);

                bindPage();
            }
        }

        private void bindPage()
        {
            DataSet ds = SupserDao.getDealInfo(deal_no);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                lblCardno.Text = ds.Tables[0].Rows[0]["card_no"].ToString();
                lblCpartyName.Text = ds.Tables[0].Rows[0]["cparty_name"].ToString();
                txtDue_Amount.Text = ds.Tables[0].Rows[0]["due_amount"].ToString();
                lblEntity.Text = ds.Tables[0].Rows[0]["entity"].ToString();
                lblInitial_amount.Text = ds.Tables[0].Rows[0]["initial_amount"].ToString();

                txtDue_date.Text = ds.Tables[0].Rows[0]["due_date"].ToString();
                txtDueProdRemark.Text = ds.Tables[0].Rows[0]["due_prodremark"].ToString();
                txtPerson.Text = ds.Tables[0].Rows[0]["designee"].ToString();
                txtReview_date.Text = ds.Tables[0].Rows[0]["review_date"].ToString();
                txtSeller.Text = ds.Tables[0].Rows[0]["dealer"].ToString();
                txtValue_date.Text = ds.Tables[0].Rows[0]["value_date"].ToString();

                dropProd.Items.FindByValue(ds.Tables[0].Rows[0]["prod_code"].ToString()).Selected = true;
                dropDueProd.Items.FindByValue(ds.Tables[0].Rows[0]["due_prodcode"].ToString()).Selected = true;

                txt_should_amount.Text = ds.Tables[0].Rows[0]["should_amount"].ToString();
                txt_stand_discount.Text = ds.Tables[0].Rows[0]["stand_discount"].ToString();
                txt_real_discount.Text = ds.Tables[0].Rows[0]["real_discount"].ToString();
                txt_amount_spread.Text = ds.Tables[0].Rows[0]["amount_spread"].ToString();
            }
            else
            {
                lblWarning.Text = "不存在此交易！";
                btnUpdate.Enabled = false;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            bool bok = false;
            try
            {
                bok = SupserDao.UpdateDeal(deal_no,
                  dropProd.SelectedItem.Value.Trim(),
                  dropDueProd.SelectedItem.Value.Trim(),
                  txtDueProdRemark.Text.Trim(),
                  txt_should_amount.Text.Trim(),
                  txt_stand_discount.Text.Trim(),
                  txt_real_discount.Text.Trim(),
                  txt_amount_spread.Text.Trim(),
                  txtDue_Amount.Text.Trim(),
                  txtValue_date.Text.Trim(),
                  txtDue_date.Text.Trim(),
                  txtReview_date.Text.Trim(),
                  txtSeller.Text.Trim(),
                  txtPerson.Text.Trim());
                if (bok)
                    lblWarning.Text = "更新成功！";
                else
                    lblWarning.Text = "更新失败！";
            }
            catch (Exception ex)
            {
                lblWarning.Text = "更新失败！" + ex.ToString();
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("UpdateSearch.aspx", true);
        }
    }
}